namespace Profit.ViewModels
{
    using System;
    using System.Windows.Input;
    using Core.Base;
    using Interfaces;
    using Views.Interfaces;

    /// <summary>
    /// View model base class
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class ViewModelBase<T>  : PropertyChangedBase, IViewModel where T : class, IView
    {
        private readonly T _view;
        private ICommand _validateCommand = null;
        /// <summary>
        /// Creates a new instance
        /// </summary>
        /// <param name="view">The IView instance associated</param>
        protected ViewModelBase(T view)
        {
            if(view == null)throw new ArgumentNullException("view");
            _view = view;
            _view.DataContext = this;
        }
        /// <summary>
        /// Obtains the associated view
        /// </summary>
        public IView View
        {
            get { return _view; }
        }

        /// <summary>
        /// Obtains the validation command
        /// </summary>
        public virtual ICommand ValidateCommand
        {
            get { return _validateCommand ?? (_validateCommand = new BindableCommand<object>(OnValidate)); }
        }

        internal virtual void OnValidate(object value)
        {
            if(View.Owner!=null)
                View.DialogResult = true;
            View.Close();
        }
    }
}